using System;
using System.Drawing;
using UnityEngine;
using QFramework;
using UnityEngine.Tilemaps;

namespace ProjectSurvival
{
    public partial class RepeatTileController : ViewController
    {
// #if UNITY_EDITOR
//         [UnityEditor.CustomEditor(typeof(RepeatTileController))]
//         public class RepeatileControllerEditor : UnityEditor.Editor
//         {
//             public override void OnInspectorGUI()
//             {
//                 base.OnInspectorGUI();
//                 if (GUILayout.Button("重新计算边界"))
//                 {
//                     var controller = target as RepeatTileController;
//                     controller.Tilemap.CompressBounds();
//                 }
//             }
//         }
// #endif
        private Tilemap _up;
        private Tilemap _down;
        private Tilemap _left;
        private Tilemap _right;
        private Tilemap _leftUp;
        private Tilemap _leftDown;
        private Tilemap _rightUp;
        private Tilemap _rightDown;
        private Tilemap _center;

        private int AreaX = 0;
        private int AreaY = 0;
        
        private float _timer = 0f;

        void CreatTileMaps()
        {
            _up = Tilemap.InstantiateWithParent(transform);
            _down = Tilemap.InstantiateWithParent(transform);
            _left = Tilemap.InstantiateWithParent(transform);
            _right = Tilemap.InstantiateWithParent(transform);
            _leftUp = Tilemap.InstantiateWithParent(transform);
            _leftDown = Tilemap.InstantiateWithParent(transform);
            _rightUp = Tilemap.InstantiateWithParent(transform);
            _rightDown = Tilemap.InstantiateWithParent(transform);
            _center = Tilemap;
        }

        void UpdateTilePosition()
        {
            _up.Position(new Vector3(AreaX * Tilemap.size.x, (AreaY + 1) * Tilemap.size.y));
            _down.Position(new Vector3(AreaX * Tilemap.size.x, (AreaY - 1) * Tilemap.size.y));
            _left.Position(new Vector3((AreaX-1) * Tilemap.size.x, AreaY * Tilemap.size.y));
            _right.Position(new Vector3((AreaX+1) * Tilemap.size.x, AreaY * Tilemap.size.y));
            _leftUp.Position(new Vector3((AreaX-1) * Tilemap.size.x, (AreaY+1) * Tilemap.size.y));
            _leftDown.Position(new Vector3((AreaX-1) * Tilemap.size.x, (AreaY-1) * Tilemap.size.y));
            _rightUp.Position(new Vector3((AreaX+1) * Tilemap.size.x, (AreaY+1) * Tilemap.size.y));
            _rightDown.Position(new Vector3((AreaX+1) * Tilemap.size.x, (AreaY-1) * Tilemap.size.y));
            _center.Position(new Vector3((AreaX) * Tilemap.size.x, (AreaY) * Tilemap.size.y));
        }

        void Start()
        {
            CreatTileMaps();
        }

        private void Update()
        {
            _timer += Time.deltaTime;
            if (Player.Instance&&_timer>1)
            {
                _timer = 0;
                var cellPos = Tilemap.layoutGrid.WorldToCell(Player.Instance.transform.position);
                AreaX = cellPos.x / Tilemap.size.x;
                AreaY = cellPos.y / Tilemap.size.y; 
                UpdateTilePosition();
            }
        }
    }
}